package com.paradise.ytj.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.RadioButton;
import android.widget.TextView;

import com.paradise.ytj.R;
import com.paradise.ytj.bean.AgreeEvent;
import com.paradise.ytj.bean.HangupEvent;
import com.paradise.ytj.utils.CameraUtils;
import com.paradise.api.FaceCall;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2017/8/24 0024.
 */

public class WaitFragment extends Fragment implements View.OnClickListener, View.OnFocusChangeListener {

    private View inflate;
    private RadioButton ivAgree;
    private RadioButton ivDisAgree;
    private String object;
    private String inout;
    private TextView tvObject;
    private TextView tvInOut;
    private Activity context;
    private String method;
    private TextView tvCamera;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        inflate = View.inflate(context, R.layout.fragment_wait, null);
        initView();
        initData();
        initListener();
        return inflate;
    }

    private void initView() {
        ivAgree = (RadioButton) inflate.findViewById(R.id.iv_agree);
        ivDisAgree = (RadioButton) inflate.findViewById(R.id.iv_disagree);
        tvObject = (TextView) inflate.findViewById(R.id.tv_object);
        tvInOut = (TextView) inflate.findViewById(R.id.tv_inout);
        tvCamera = (TextView) inflate.findViewById(R.id.tv_camera);
        ivAgree.setFocusable(true);

    }

    private void initData() {
        Bundle arguments = getArguments();
        inout = arguments.getString("inout");
        object = arguments.getString("object");
        method = arguments.getString("method");

        if (inout == null) {
            inout = "out";
        }
        if (object == null) {
            object = "";
        }
        if (method == null) {
            method = "video";
        }
        if (inout.equals("in")) {
            if (method.equals("video")) {

                tvObject.setText(object);
            } else if (method.equals("audio")) {
                tvObject.setText(object);
            }
            tvInOut.setText(R.string.other_party_invites_video);
        } else if (inout.equals("out")) {
            ivAgree.setVisibility(View.GONE);
            tvObject.setText(object);
            tvInOut.setText(R.string.waiting_for_an_invitation);
        }

        if (CameraUtils.getNumber() == 0) {
            ivAgree.setVisibility(View.GONE);
            ivDisAgree.setVisibility(View.GONE);
            tvCamera.setVisibility(View.VISIBLE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(4000);
                    //终止通话
                    FaceCall.instance().terminateCurrentCall();
                    HangupEvent hangupEvent = new HangupEvent();
                    EventBus.getDefault().post(hangupEvent);
                }
            }).start();
        }
    }

    private void initListener() {

        ivAgree.setOnClickListener(this);
        ivDisAgree.setOnClickListener(this);

        ivAgree.setOnFocusChangeListener(this);
        ivDisAgree.setOnFocusChangeListener(this);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_agree:
                if (inout.equals("in")) {
                    AgreeEvent agreeEvent = new AgreeEvent();
                    EventBus.getDefault().post(agreeEvent);
                }
                break;
            case R.id.iv_disagree:
                //终止通话
                FaceCall.instance().terminateCurrentCall();
                HangupEvent hangupEvent = new HangupEvent();
                EventBus.getDefault().post(hangupEvent);
                break;
        }
    }

    @Override
    public void onFocusChange(View view, boolean b) {

        switch (view.getId()) {
            case R.id.iv_agree:
                if (b) {
                    ScaleAnimation animation = new ScaleAnimation(1f, 1.15f, 1f, 1.15f, 0.5f, 0.5f);
                    animation.setDuration(200);
                    animation.setFillAfter(true);
                    ivAgree.startAnimation(animation);
                } else {
                    ScaleAnimation animation = new ScaleAnimation(1.15f, 1f, 1.15f, 1f, 0.5f, 0.5f);
                    animation.setDuration(200);
                    animation.setFillAfter(true);
                    ivAgree.startAnimation(animation);
                }
                break;
            case R.id.iv_disagree:
                if (b) {
                    ScaleAnimation animation = new ScaleAnimation(1f, 1.15f, 1f, 1.15f, 0.5f, 0.5f);
                    animation.setDuration(200);
                    animation.setFillAfter(true);
                    ivDisAgree.startAnimation(animation);
                } else {
                    ScaleAnimation animation = new ScaleAnimation(1.15f, 1f, 1.15f, 1f, 0.5f, 0.5f);
                    animation.setDuration(200);
                    animation.setFillAfter(true);
                    ivDisAgree.startAnimation(animation);
                }
                break;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
